// app.js
App({
  d: {
    hostUrl: 'https://www.lh1717.com',
    // ceshiUrl: 'http://10.131.129.1:8080/ADPSys',
    ceshiUrl: 'https://www.lh1717.com/ADPSys',
    // ceshiUrl: 'http://192.168.1.125:8080/ADPSys',
    Cookie: '',
  },
  globalData: {
    userInfo: null,
    openId:null,
    carts: null,
    header: {
      // 'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': ''
    },
    contact: [],
    isGetCookie: '',
  },

  onLaunch: function() {
    //调用API从本地缓存中获取数据
    var that = this;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    //login
    wx.login({
      success: res => {
        // console.log("res.code");
        // console.log(res.code);
        that.getopenId(res.code)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId  
        that.getUserInfo();
      }
    })
  },
  getopenId: function (code) {
    var that = this;
    // console.log('userLogin');
    wx.request({
      url:  'https://www.lh1717.com/Wxzf/Getopenid',
      method: 'get',
      data: {
        "code": code,
      },
      header: that.globalData.header,
      success: function (res) {
        var data = res.data;
        console.log(res.data.openid)
        console.log("openid")
        that.globalData.openId = res.data.openid;
        if (data.status == 0) {
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
        console.log(that.globalData.openId)
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！7333',
          duration: 2000
        });
      },
    });
  },

  getUserInfo: function(cb) {
    var that = this;
    // console.log("getUserInfo")
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口      
      wx.login({
        success: function(res) {
          var code = res.code;
          // console.log("getUserInfo")
          // console.log(res)
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({
                  success: function(res) {
                    console.log(res)
                    that.globalData.userInfo = res.userInfo;
                    typeof cb == "function" && cb(that.globalData.userInfo);
                    that.userLogin();
                  }
                })
              } else {
                wx.reLaunch({
                  url: '/pages/index/authorize',
                })
              }
            }
          })
        }
      });
    }
  },
  userLogin: function() {
    var that = this;
    wx.request({
      url: this.d.ceshiUrl + '/user/UserMgrAction!login',
      method: 'get',
      data: {
        "user.userName": 'b01',
        "user.password": 123456
      },

      header: that.globalData.header,
      success: function(res) {
        var data = res.data;
        console.log(res)
        console.log("res")
        if (data.status == 0) {
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
        var cookie = res.header["Set-Cookie"];
        that.globalData.header.Cookie = cookie;
        that.getLoginUser();
      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！7333',
          duration: 2000
        });
      },
    });
  },

  getLoginUser: function() {
    var that = this;
    // console.log('getLoginUser');
    wx.request({
      url: this.d.ceshiUrl + '/user/UserMgrAction!getLoginUser',
      method: 'post',
      header: that.globalData.header,
      success: function(res) {
        that.getContact();
        // console.log(res);
        // console.log(res.data.msg);
        var data = res.data;
        if (data.status == 0) {
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！73',
          duration: 2000
        });
      },
    });
  },
  getContact: function() {
    var that = this;
    // console.log('getContact');
    wx.request({
      url: this.d.ceshiUrl + '/transaction/ContractMgrAction!listByBuyer',
      method: 'get',
      header: that.globalData.header,
      success: function(res) {
        that.seclectContract(res.data.rows[1].id);
        var data = res.data;
        // console.log(res)
        // console.log('getContact11');
        if (data.status == 0) {
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
        var contact = res.data.rows;
        var contectFactor = { "id": 0, "name": "查看厂家" };
        contact.push(contectFactor);
        that.globalData.contact = contact;
        that.globalData.isGetCookie = 'true';

      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！73',
          duration: 2000
        });
      },
    });
  },

  seclectContract: function(contractId) {
    var that = this;
    wx.request({
      url: that.d.ceshiUrl + '/transaction/ContractMgrAction!selectById',
      method: 'get',
      data: {
        contractId: contractId,
      },

      header: that.globalData.header,
      success: function(res) {
        var obj1 = res.data.obj;
        console.log("seclectContract")
        console.log(res);
      },
      fail: function(e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  }
});